package com.ds.infrastructure.hyperspace.console.service.impl;

import com.ds.infrastructure.hyperspace.console.dao.dao.TbMetricsTaskDao;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbMetricsTask;
import com.ds.infrastructure.hyperspace.console.entity.where.TbCustomWhere;
import com.ds.infrastructure.hyperspace.console.entity.where.TbMetricsTaskCustomWhere;
import com.ds.infrastructure.hyperspace.console.service.inf.IMetricsTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-11-01 09:25
 **/

@Service
public class MetricsTaskService implements IMetricsTaskService {
    @Autowired
    TbMetricsTaskDao tbMetricsTaskDao;

    @Override
    public int save(TbMetricsTask record) {
        return tbMetricsTaskDao.insertSelective(record);
    }

    @Override
    public int update(TbMetricsTask record) {
        return tbMetricsTaskDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public TbMetricsTask get(Long id) {
        TbMetricsTask entity = tbMetricsTaskDao.selectByPrimaryKey(id);
        if (entity != null && TbPublicEnum.GMD_IS_DEL.NO.code.equals(entity.getGmtIsDeleted())) {
            return entity;
        }
        return null;
    }

    @Override
    public int delete(Long id) {
        TbMetricsTask item = new TbMetricsTask();
        item.setId(id);
        item.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.YES.code);
        return update(item);
    }

    @Override
    public List<TbMetricsTask> getList(TbMetricsTask where, TbCustomWhere customWhere, TbPageWhere pageWhere) {
        return tbMetricsTaskDao.selectListForPage(where, customWhere, pageWhere);
    }

    @Override
    public int getTotal(TbMetricsTask where, TbCustomWhere customWhere) {
        return tbMetricsTaskDao.selectListForPageCount(where, customWhere);
    }

    @Override
    public TbMetricsTask selectOne(TbMetricsTask where, TbCustomWhere customWhere) {
        List<TbMetricsTask> itemList = tbMetricsTaskDao.selectListForPage(where, customWhere, TbPageWhere.newPage4OnlyOne());
        return itemList.stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public TbMetricsTask selectLastOne(TbMetricsTask where, TbCustomWhere customWhere) {
        if (customWhere == null) {
            customWhere = new TbMetricsTaskCustomWhere();
        }
        customWhere.setOrderByType(TbPublicEnum.ORDER_BY_TYPE.ID_DESC.value);
        return selectOne(where, customWhere);
    }

    @Override
    public void clean(Long executeTime4EndTime) {
        TbMetricsTaskCustomWhere customWhere = new TbMetricsTaskCustomWhere().setExecuteTime4EndTime(executeTime4EndTime);
        tbMetricsTaskDao.delete(customWhere);
    }
}
